package com.ruoyi.quartz.task;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.GlobalSettingsType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.out.domain.AnnualLeaveRecord;
import com.ruoyi.out.mapper.AnnualLeaveRecordMapper;
import com.ruoyi.system.mapper.GeneralConfigurationMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Yuan
 * @description
 * @date 2022/10/25 14:42
 */
@Component("annualLeaveCalculationTask")
public class AnnualLeaveCalculationTask {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private GeneralConfigurationMapper generalConfigurationMapper;
    @Autowired
    private AnnualLeaveRecordMapper annualLeaveRecordMapper;

    /**
     * 定时任务，计算年休假时长（可设置为每天凌晨两点执行）
     */
    @Transactional
    public void calculateAnnualLeave(){
        //根据当前日期，以及入职时间判断用户是否具有年休假的资格
        List<SysUser> sysUsers = sysUserMapper.selectAllUserList();
        //获取当前时间
        Date nowDate = DateUtils.getNowDate();
        String year = DateUtils.parseDateToStr("yyyy",nowDate);
        //计算一年的time
        long yearTime = 1000 * 60 * 60 * 24 * 365L;
        BigDecimal overOneLessTen = new BigDecimal(generalConfigurationMapper.selectHolidaySettingByType(GlobalSettingsType.ONE_GRADE_YEAR_LEAVE.getType()));
        BigDecimal overTenLessTwenty = new BigDecimal(generalConfigurationMapper.selectHolidaySettingByType(GlobalSettingsType.TWO_GRADE_YEAR_LEAVE.getType()));
        BigDecimal overTwenty = new BigDecimal(generalConfigurationMapper.selectHolidaySettingByType(GlobalSettingsType.THREE_GRADE_YEAR_LEAVE.getType()));
        List<AnnualLeaveRecord> annualLeaveRecordList = new ArrayList<>();
        for (SysUser sysUser : sysUsers) {
            if(sysUser.getEntryTime() == null){
                continue;
            }
            long diffTime = nowDate.getTime() - sysUser.getEntryTime().getTime();
            if(diffTime >= yearTime && diffTime < yearTime * 10){
                AnnualLeaveRecord annualLeaveRecord = new AnnualLeaveRecord();
                annualLeaveRecord.setUserId(sysUser.getUserId());
                annualLeaveRecord.setAnnualLeaveDay(overOneLessTen);
                annualLeaveRecord.setYear(year);
                annualLeaveRecordList.add(annualLeaveRecord);

            }else if(diffTime >= yearTime * 10 && diffTime < yearTime * 20){
                AnnualLeaveRecord annualLeaveRecord = new AnnualLeaveRecord();
                annualLeaveRecord.setUserId(sysUser.getUserId());
                annualLeaveRecord.setAnnualLeaveDay(overTenLessTwenty);
                annualLeaveRecord.setYear(year);
                annualLeaveRecordList.add(annualLeaveRecord);
            }else if(diffTime >= yearTime * 20){
                AnnualLeaveRecord annualLeaveRecord = new AnnualLeaveRecord();
                annualLeaveRecord.setUserId(sysUser.getUserId());
                annualLeaveRecord.setAnnualLeaveDay(overTwenty);
                annualLeaveRecord.setYear(year);
                annualLeaveRecordList.add(annualLeaveRecord);
            }
        }
        if(annualLeaveRecordList.size() > 0){
            List<AnnualLeaveRecord> annualLeaveRecords = annualLeaveRecordMapper.selectAnnualLeaveRecordList();
            //过滤掉数据库中已存在的记录
            annualLeaveRecordList = annualLeaveRecordList.stream().filter(a-> annualLeaveRecords.stream().allMatch(b-> !(a.getUserId().equals(b.getUserId()) && a.getYear().equals(b.getYear())))).collect(Collectors.toList());
            //将数据批量插入数据库中
            if(annualLeaveRecordList.size() > 0){
                annualLeaveRecordMapper.insertBatch(annualLeaveRecordList);
            }
        }
    }

}
